package fragment;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.TextView;

import com.example.administrator.animation_demo.R;

public class RotationAnimatorFragment extends Fragment implements View.OnClickListener {

    private View mView;
    private TextView mTv;
    private Button mBtnS;
    private Button mBtnE;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = View.inflate(getActivity(), R.layout.fragment_rotation_animator, null);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(mView);
        return mView;
    }

    private void initView(View view) {
        mTv = (TextView) view.findViewById(R.id.tv);

        mBtnS = (Button) view.findViewById(R.id.start);
        mBtnE = (Button) view.findViewById(R.id.sut);
        mBtnE.setOnClickListener(this);
        mBtnS.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.start:
                addAnimat(mTv,1);
                break;
            case R.id.sut:
                addAnimat(mTv,4);
                break;
        }
    }


    public void addAnimat(View view,int type){
        switch (type){
            case 1:
                ObjectAnimator ax=ObjectAnimator.ofFloat(view,"rotationX",0,360);
                ax.setDuration(5000);
                ax.setInterpolator(new LinearInterpolator());
                ax.setRepeatMode(ValueAnimator.REVERSE);
                ax.start();
                break;
            case 2:
                ObjectAnimator ay=ObjectAnimator.ofFloat(view,"rotationY",0,359);
                ay.setDuration(5000);
                ay.setInterpolator(new LinearInterpolator());
                ay.setRepeatMode(ValueAnimator.REVERSE);
                ay.start();
                break;
            case 3:
                ObjectAnimator az = ObjectAnimator.ofFloat(view,"rotation",0,360);
                az.setDuration(5000);
                az.setInterpolator(new LinearInterpolator());
                az.setRepeatMode(ValueAnimator.REVERSE);
                az.start();
                break;
            case 4:
                ObjectAnimator a = ObjectAnimator.ofFloat(view,"rotation",0,360);
                ObjectAnimator b = ObjectAnimator.ofFloat(view,"rotationx",0,360);
                ObjectAnimator c = ObjectAnimator.ofFloat(view,"rotationY",0,359);
                AnimatorSet set = new AnimatorSet();
                set.play(a).with(b).with(c);
                set.setDuration(5000);
                set.start();
                break;
        }
    }
}
